package UILag;
import java.util.Scanner;
import CTRLag.*;
import ModelLag.*;
 

public class KursusUI
{
    private KursusCTR kursusCTR;    //reference to the ControlLayer
    
    //constructor which does nothing
        
        public KursusUI()
    {
        // initialise instance variables
        kursusCTR = new KursusCTR();
    }

    public void start()
    {
        kursusMenu();
    }
    public void kursusMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeKursusMenu(); 
          if(choice == 1)
          {   //opret kursus
              opretKursus();
              
          }
          else{ //find kursusid
              if(choice == 2){
                Kursus k=findKursus();
                System.out.println(" Kursets id er:" + k.getId());
                     
              } 
          else {
              if(choice == 3) {
                  opretAfholdelse();
                }
              else{
                exit = true;
             }//end else
               
         }//end else
        }//end else    
       }//end while
       
    }
    
    public int writeKursusMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("*** Kursus Menu ***");
            System.out.println(" (1) Opret et kursus");
            System.out.println(" (2) Find et kursus");
            System.out.println(" (3) Opret afholdelse af et kursus");
            System.out.println(" (4) Afslut");
            System.out.print("\n Vælg: ");
            
            int choice = keyboard.nextInt();
            return choice;
        }
        //the method reads id for a person and return
    // 
    public int inputId()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Angiv id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Input skal være et tal - prøv igen");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    public String inputTitel()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("Angiv kursets navn:  ");
         String title = keyboard.nextLine();
         return title;
    }
   
         
    public String inputBeskrivelse()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv en beskrivelse af kurset: ");
        String beskrivelse = keyboard.nextLine();
        return beskrivelse;
    }
    
    public String inputDato()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Angiv en dato for afholdelse af kurset: ");
        String dato = keyboard.nextLine();
        return dato;
    }
    
    public boolean inputStatus()
    {
        boolean ok = false, status = false;
        String check = "";
        
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Er kurset godkendt til afholdelse:  ");
           try{
             check = keyboard.nextLine();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Input skal være ja eller nej - prøv igen");
             String input = keyboard.nextLine();
           }
        }
        if (check.equals("ja")) {
            status = true;
            return status;
        }
        return status;
    }
    
    /**
     * Hvilket kursus skal sættes til afholdelse
     * @param int id
     */
    public Kursus inputKursus()
    {
     int id = 0;
     boolean ok = false;
     
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Angiv id for det kursus der skal afholdes: ");
            try{
                id = keyboard.nextInt();
                ok = true;
            } 
            catch (Exception e)
            {
                System.out.println("Input skal være et gyldigt id");
                int input = keyboard.nextInt();
            }
        }
      
     Kursus k = kursusCTR.findKursus(id);
     return k;
    }
    
    public void showId(int id)
    {
        System.out.println("Kursets id: " + id);
    }
    public void showTitel(String titel)
    { 
        System.out.println("Kursets navn: " + titel);
    }
    public void showBeskrivelse(String beskrivelse)
    { 
        System.out.println("Kursets dato: " + beskrivelse);
    }

    private Kursus findKursus()
    {//is to be changed when the address book controller is completed
        int id = inputId();
        Kursus k = (kursusCTR.findKursus(id));
        return k;
    } 
    
    private void opretAfholdelse()
    {
        int id = inputId();
        String dato = inputDato();
        boolean status = inputStatus();
        Kursus kursus = inputKursus();
        
        kursusCTR.opretAfholdelse(id, dato, status, kursus);
    }
    
    private void opretKursus()
    {
        int id = inputId();
        String navn = inputTitel();
        String dato = inputBeskrivelse();
        
        kursusCTR.opretKursus(id, navn, dato);
    }  
}
